package client;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 * Main Launcher for client.
 */
public class Launcher extends JFrame {
	
	private static final long serialVersionUID = -3914443013724510940L;
	private static final Logger logger = Logger.getLogger(Launcher.class.getName());

	private static final int width = 300;
	private static final int height = 100;
	
	
	private JButton signinButton;
	private JLabel userLabel = new JLabel("Username:");
	private JLabel passLabel = new JLabel("Password:");
	private JTextField userField = new JTextField("", 50);
	private JPasswordField passFeild = new JPasswordField("", 50);

	private Launcher() {
		super("UFSE");
		setSize(width, height);
		setResizable(false);
		setupLayout();
		setLocationRelativeTo(null);
		
		addWindowListener(new WindowCloser());
	}
	
	private void setupLayout() {
		Container c = getContentPane();
		c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
		
		signinButton = new JButton("Sigin");
		signinButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				singinPerformed(e);
			}
		});
		
		Box topContainer = new Box(BoxLayout.X_AXIS);
		topContainer.add(Box.createHorizontalStrut(5));
		topContainer.add(userLabel);
		topContainer.add(Box.createHorizontalGlue());
		topContainer.add(userField);
		topContainer.add(Box.createHorizontalStrut(5));
		
		Box midContainer = new Box(BoxLayout.X_AXIS);
		midContainer.add(Box.createHorizontalStrut(5));
		midContainer.add(passLabel);
		midContainer.add(Box.createHorizontalGlue());
		midContainer.add(passFeild);
		midContainer.add(Box.createHorizontalStrut(5));

		c.add(topContainer, BorderLayout.NORTH);
		c.add(midContainer, BorderLayout.CENTER);
		c.add(signinButton, BorderLayout.SOUTH);
	}
	
	private void singinPerformed(ActionEvent e) {
		String username = userField.getText();
		String password = passFeild.getPassword().toString();
		
		if (username == null || username.isEmpty() || password == null || password.isEmpty())
			return;
	}
	
	public static void main(String[] args) {
		Launcher mainLauncher;
		
		try {
			mainLauncher = new Launcher();
			mainLauncher.setVisible(true);
		} catch (Exception ex) {
			logger.log(Level.SEVERE, "Critical Exception", ex);
			return;
		}
	}

}
